package org.cerberus.platform.components;

import static org.cerberus.platform.component.ComponentUtilities.attributeChanged;

import java.util.List;

import org.cerberus.platform.component.ComponentUtilities;
import org.cerberus.platform.component.annotations.PAttribute;
import org.cerberus.platform.component.annotations.PComponent;

@PComponent(classID="PActionGroup")
public class PActionGroup {

	private static final String ATTRIBUTE_NAME = "name";
	private static final String ATTRIBUTE_DESCRIPTION = "description";
	private static final String ATTRIBUTE_COMPONENTS = "components";
	
	@PAttribute
	private String name;
	
	@PAttribute
	private String description;
	
	@PAttribute
	private List<Object> components;
	
	public PActionGroup(String id) {
		ComponentUtilities.init(this, id);
	}
	
	public List<Object> getComponents() {
		return components;
	}

	public void addAction(PAction action) {
		components.add(action);
		attributeChanged(this, ATTRIBUTE_COMPONENTS);
	}
	
	public void addActionGroup(PActionGroup actionGroup) {
		components.add(actionGroup);
		attributeChanged(this, ATTRIBUTE_COMPONENTS);
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		attributeChanged(this, ATTRIBUTE_NAME);
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
		attributeChanged(this, ATTRIBUTE_DESCRIPTION);
	}

	public void updateState() {
		for (Object component : this.components) {
			if (component instanceof PAction) {
				((PAction) component).updateState();
			} else if (component instanceof PActionGroup) {
				((PActionGroup) component).updateState();
			}
		}
	}
	
}
